#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <sys/eventfd.h>

void *wait(void *args)
{
    int evfd = *static_cast<int *>(args);
    while (1)
    {
        uint64_t flag;
        std::cout << "reading!" << std::endl;
        ssize_t n = ::read(evfd, &flag, sizeof(flag));
        std::cout << "wait success! errno:" << errno << std::endl;
    }

    return nullptr;
}

void *wakeup(void *args)
{
    int evfd = *static_cast<int *>(args);
    while (1)
    {
        errno++;
        uint64_t flag = 1;
        ssize_t n = ::write(evfd, &flag, sizeof(flag));
        std::cout << "wakeup! evfd:" << evfd << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    int evfd = eventfd(0, 0);
    pthread_t t1, t2;
    pthread_create(&t1, nullptr, wait, (void *)&evfd);
    pthread_create(&t2, nullptr, wakeup, (void *)&evfd);

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);

    ::close(evfd);
    return 0;
}